package com.kaola.service.impl;

import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import com.kaola.dao.UserProductDAO;
import com.kaola.pojo.User;
import com.kaola.pojo.UserProduct;
import com.kaola.service.UserProductService;

public class UserProductServiceImpl implements UserProductService {
	
	private UserProductDAO userProductDAO;
	
	public UserProductDAO getUserProductDAO() {
		return userProductDAO;
	}

	public void setUserProductDAO(UserProductDAO userProductDAO) {
		this.userProductDAO = userProductDAO;
	}

	public void addToShoppingCart(UserProduct userProduct) {
		userProductDAO.add(userProduct);
	}
	
	public List<UserProduct> listForShoppingCart(User user) {
		return userProductDAO.list(user);
	}

	public void updateToShoppingCart(UserProduct userProduct) {
		userProductDAO.modify(userProduct);
	}

	public void removeFromShoppingCart(int upid) {
		userProductDAO.delete(upid);
	}

	public UserProduct get(int upid) {
		return userProductDAO.get(upid);
	}
	
	public boolean isExist(int uid, int pid) {
		if (userProductDAO.get(uid, pid) != null) {
			return true;
		}
		return false;
	}

	public int correctionBuyNum(int buyNum, int inventory) {
		if (buyNum <= 0) {
			buyNum = 1;
		}
		if (buyNum > inventory) {
			buyNum = inventory;
		}
		return buyNum;
	}
}
